package com.leqicheng.tenon.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TenonDB extends SQLiteOpenHelper {
    private final static String DATABASE_NAME = "Tenon.db";
    private final static int DATABASE_VERSION = 1;
    private final static String TABLE_NAME = "userinfo_table";
    public final static String TENON_ID = "tenon_id";
    public final static String CHANNEL_KEY = "channel_key";
    public final static String GAME_KEY = "game_key";
    public final static String TENON_NAME = "tenon_name";
    public final static String TENON_ICON = "tenon_icon";
    public final static String TENON_SEX = "tenon_sex";

    public TenonDB(Context context) {
        // TODO Auto-generated constructor stub
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // 创建table
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE " + TABLE_NAME + " (" + TENON_ID
                + " INTEGER , " + CHANNEL_KEY + " text, " + TENON_NAME
                + " text, " + TENON_ICON + " text, " + TENON_SEX + " text, "
                + GAME_KEY + " text, PRIMARY KEY(" + TENON_ID + ","
                + CHANNEL_KEY + "," + GAME_KEY + ")) ;";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        this.onCreate(db);
    }

    public Cursor select() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db
                .query(TABLE_NAME, null, null, null, null, null, null);
        return cursor;
    }

    // 增加操作
    public long insert(String tenonId, String channelId, String gameId,
            String tenonName, String tenonIcon, String tenonSex) {
        SQLiteDatabase db = this.getWritableDatabase();
        /* ContentValues */
        ContentValues cv = new ContentValues();
        cv.put(TENON_ID, tenonId);
        cv.put(CHANNEL_KEY, channelId);
        cv.put(TENON_NAME, tenonName);
        cv.put(TENON_ICON, tenonIcon);
        cv.put(TENON_SEX, tenonSex);
        long row = db.insert(TABLE_NAME, null, cv);
        return row;
    }

    // 删除操作
    public void delete(String tenonId) {
        SQLiteDatabase db = this.getWritableDatabase();
        String where = TENON_ID + " = ?";
        String[] whereValue = { tenonId };
        db.delete(TABLE_NAME, where, whereValue);
    }

    // 修改操作
    public void update(String tenonId, String channelId, String gameId) {
        SQLiteDatabase db = this.getWritableDatabase();
        String where = TENON_ID + " = ?";
        String[] whereValue = { tenonId };

        ContentValues cv = new ContentValues();
        cv.put(CHANNEL_KEY, channelId);
        db.update(TABLE_NAME, cv, where, whereValue);
    }

    public String count(String channelId, String gameId) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] args = { channelId};
        Cursor c = db.rawQuery("SELECT * FROM userinfo_table WHERE "
                + CHANNEL_KEY + " =? ", args);
        if (c.moveToFirst()) {// 判断游标是否为空
            String tenonId = c.getString(c.getColumnIndex(TENON_ID));
            return tenonId;
        }
        return null;
    }

    public String[] getTenonUserInfo(String tenonUserId) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] args = { tenonUserId };
        Cursor c = db.rawQuery("SELECT * FROM userinfo_table WHERE " + TENON_ID
                + "= ?", args);
        if (c.moveToFirst()) {// 判断游标是否为空
            String[] tenonUserInfo = new String[3];
            tenonUserInfo[0] = c.getString(c.getColumnIndex(TENON_NAME));
            tenonUserInfo[1] = c.getString(c.getColumnIndex(TENON_ICON));
            tenonUserInfo[2] = c.getString(c.getColumnIndex(TENON_SEX));
            return tenonUserInfo;
        }
        return null;
    }
}
